package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.QRCodeUtil;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.service.AppVersionService;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping(value = "app")
public class AppDownloadController {

    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private AppVersionService appVersionService;

    @RequestMapping("download")
    public String download(Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("download")) {
            Setting setting = settingUtils.get();
            String androidDriver = setting.getAndroidDriver();
            String androidCustomer = setting.getAndroidCustomer();
            model.addAttribute("androidDriver", androidDriver);
            model.addAttribute("androidCustomer", androidCustomer);
            return "download/view";
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "qrCode/{name}", method = RequestMethod.GET)
    public void qrCode(@PathVariable String name, HttpServletResponse response) throws IOException {
        Setting setting = settingUtils.get();
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        if ("androidDriver".equals(name)) {
            QRCodeUtil.generateQRImage(setting.getAndroidDriver(), 0xFFFFC125, out);
        } else {
            QRCodeUtil.generateQRImage(setting.getAndroidCustomer(), 0xFF48D1CC, out);
        }
    }
}
